package com.javaweb.controller;

import com.javaweb.service.dto.ArticleConditionDto;
import com.javaweb.service.dto.ArticleDto;
import com.javaweb.service.dto.ArticleUpdateDto;
import com.javaweb.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blog-api/v0.1/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    // 根据条件查询文章
    @PostMapping("/list")
    public ResponseEntity<List<ArticleDto>> list(@RequestBody ArticleConditionDto articleCond) {
        List<ArticleDto> articles = articleService.findArticlesByCondition(articleCond);
        return ResponseEntity.ok(articles);
    }

    // 新增文章
    @PostMapping("/add")
    public ResponseEntity<ArticleDto> add(@RequestBody ArticleDto articleDto) {
        ArticleDto createdArticle = articleService.addArticle(articleDto);
        return ResponseEntity.ok(createdArticle);
    }

    // 更新文章
    @PostMapping("/update")
    public ResponseEntity<ArticleUpdateDto> update(@RequestBody ArticleUpdateDto articleUpdateDto) {
        ArticleUpdateDto updatedArticle = articleService.updateArticle(articleUpdateDto);
        return ResponseEntity.ok(updatedArticle);
    }

    // 发布文章
    @GetMapping("/{id}/publish")
    public ResponseEntity<Void> publish(@PathVariable String id) {
        articleService.publishArticle(id);  // 如果需要传递用户ID来验证权限，替换null为用户ID
        return ResponseEntity.noContent().build();
    }
}
